﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Utility;
using Utility.Service;

namespace Site
{
    public partial class Agent : UserControl
    {
        public Agent()
        {
            InitializeComponent();
            this.myMenu.Logo = "1";
            this.Loaded += new RoutedEventHandler(Agent_Loaded);
        }

        void Agent_Loaded(object sender, RoutedEventArgs e)
        {
            //菜单下滑动画
            DoubleAnimation animation = new DoubleAnimation();
            animation.From = -83;
            animation.To = 0;
            animation.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animation, myMenu);
            Storyboard.SetTargetProperty(animation, new PropertyPath(Canvas.TopProperty));
            Storyboard storyboard = new Storyboard();
            storyboard.Children.Add(animation);
            storyboard.Begin();

            if (CSession.Session.ContainsKey("login") && CSession.Session["login"] != "")
            {
                this.spLogin.Visibility = Visibility.Collapsed;
                this.tbkUserName.Text = CSession.Session["login"];
                this.spLogin1.Visibility = Visibility.Visible;
            }
            

            InitFileList(); 
        }
        

        private void imgLogin_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ServiceClient client = CUtility.GetServiceClient();
            client.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(client_LoginCompleted);
            client.LoginAsync(this.tbxUserName.Text, this.tbxPwd.Password);
        }

        void client_LoginCompleted(object sender, LoginCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                if (e.Result)
                {
                    this.spLogin.Visibility = Visibility.Collapsed;
                    this.tbkUserName.Text = this.tbxUserName.Text;
                    this.spLogin1.Visibility = Visibility.Visible;

                    CSession.Session["login"] = this.tbkUserName.Text;

                    InitFileList();
                }
            }
            else
            {
                throw e.Error;
            }
        }

        private void InitFileList()
        {
            this.sp1.Children.Clear();
            this.sp2.Children.Clear();
            this.sp3.Children.Clear();
            this.sp4.Children.Clear();
            this.sp5.Children.Clear();

            ServiceClient client1 = CUtility.GetServiceClient();
            client1.ReadFilesCompleted += new EventHandler<ReadFilesCompletedEventArgs>(client1_ReadFilesCompleted);
            client1.ReadFilesAsync(1);

            ServiceClient client2 = CUtility.GetServiceClient();
            client2.ReadFilesCompleted += new EventHandler<ReadFilesCompletedEventArgs>(client2_ReadFilesCompleted);
            client2.ReadFilesAsync(2);

            ServiceClient client3 = CUtility.GetServiceClient();
            client3.ReadFilesCompleted += new EventHandler<ReadFilesCompletedEventArgs>(client3_ReadFilesCompleted);
            client3.ReadFilesAsync(3);  

            ServiceClient client4 = CUtility.GetServiceClient();
            client4.ReadFilesCompleted += new EventHandler<ReadFilesCompletedEventArgs>(client4_ReadFilesCompleted);
            client4.ReadFilesAsync(4);

            ServiceClient client5 = CUtility.GetServiceClient();
            client5.ReadFilesCompleted += new EventHandler<ReadFilesCompletedEventArgs>(client5_ReadFilesCompleted);
            client5.ReadFilesAsync(5);

            this.LayoutRoot.UpdateLayout();
        }

        void client5_ReadFilesCompleted(object sender, ReadFilesCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                if (null != e.Result)
                {
                    for (int i = 0; i < e.Result.Count; i++)
                    {
                        if (this.spLogin1.Visibility == Visibility.Visible)
                        {
                            HyperlinkButton lnk = new HyperlinkButton();
                            lnk.Content = (i+1).ToString() + "." + e.Result[i].Title;
                            lnk.FontSize = 14;
                            lnk.NavigateUri = new Uri(App.Current.Host.Source.AbsoluteUri.Substring(0, App.Current.Host.Source.AbsoluteUri.IndexOf("/ClientBin")) + "/" + e.Result[i].Path);
                            this.sp5.Children.Add(lnk);
                        }
                        else
                        {
                            TextBlock tbk = new TextBlock();
                            tbk.FontSize = 14;
                            tbk.Text = (i + 1).ToString() + "." + e.Result[i].Title;
                            this.sp5.Children.Add(tbk);
                        }
                    }
                }
            }
            else
            {
                throw e.Error;
            }
        }

        void client4_ReadFilesCompleted(object sender, ReadFilesCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                if (null != e.Result)
                {
                    for (int i = 0; i < e.Result.Count; i++)
                    {
                        if (this.spLogin1.Visibility == Visibility.Visible)
                        {
                            HyperlinkButton lnk = new HyperlinkButton();
                            lnk.Content = (i + 1).ToString() + "." + e.Result[i].Title;
                            lnk.FontSize = 14;
                            lnk.NavigateUri = new Uri(App.Current.Host.Source.AbsoluteUri.Substring(0, App.Current.Host.Source.AbsoluteUri.IndexOf("/ClientBin")) + "/" + e.Result[i].Path);
                            this.sp4.Children.Add(lnk);
                        }
                        else
                        {
                            TextBlock tbk = new TextBlock();
                            tbk.FontSize = 14;
                            tbk.Text = (i + 1).ToString() + "." + e.Result[i].Title;
                            this.sp4.Children.Add(tbk);
                        }
                    }
                }
            }
            else
            {
                throw e.Error;
            }
        }

        void client3_ReadFilesCompleted(object sender, ReadFilesCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                if (null != e.Result)
                {
                    for (int i = 0; i < e.Result.Count; i++)
                    {
                        if (this.spLogin1.Visibility == Visibility.Visible)
                        {
                            HyperlinkButton lnk = new HyperlinkButton();
                            lnk.Content = (i + 1).ToString() + "." + e.Result[i].Title;
                            lnk.FontSize = 14;
                            lnk.NavigateUri = new Uri(App.Current.Host.Source.AbsoluteUri.Substring(0, App.Current.Host.Source.AbsoluteUri.IndexOf("/ClientBin")) + "/" + e.Result[i].Path);
                            this.sp3.Children.Add(lnk);
                        }
                        else
                        {
                            TextBlock tbk = new TextBlock();
                            tbk.FontSize = 14;
                            tbk.Text = (i + 1).ToString() + "." + e.Result[i].Title;
                            this.sp3.Children.Add(tbk);
                        }
                    }
                }
            }
            else
            {
                throw e.Error;
            }
        }

        void client2_ReadFilesCompleted(object sender, ReadFilesCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                if (null != e.Result)
                {
                    for (int i = 0; i < e.Result.Count; i++)
                    {
                        if (this.spLogin1.Visibility == Visibility.Visible)
                        {
                            HyperlinkButton lnk = new HyperlinkButton();
                            lnk.Content = (i + 1).ToString() + "." + e.Result[i].Title;
                            lnk.FontSize = 14;
                            lnk.NavigateUri = new Uri(App.Current.Host.Source.AbsoluteUri.Substring(0, App.Current.Host.Source.AbsoluteUri.IndexOf("/ClientBin")) + "/" + e.Result[i].Path);
                            this.sp2.Children.Add(lnk);
                        }
                        else
                        {
                            TextBlock tbk = new TextBlock();
                            tbk.FontSize = 14;
                            tbk.Text = (i + 1).ToString() + "." + e.Result[i].Title;
                            this.sp2.Children.Add(tbk);
                        }
                    }
                }
            }
            else
            {
                throw e.Error;
            }
        }

        void client1_ReadFilesCompleted(object sender, ReadFilesCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                if (null != e.Result)
                {
                    for (int i = 0; i < e.Result.Count; i++)
                    {
                        if (this.spLogin1.Visibility == Visibility.Visible)
                        {
                            HyperlinkButton lnk = new HyperlinkButton();
                            lnk.Content = (i + 1).ToString() + "." + e.Result[i].Title;
                            lnk.FontSize = 14;
                            lnk.NavigateUri = new Uri(App.Current.Host.Source.AbsoluteUri.Substring(0, App.Current.Host.Source.AbsoluteUri.IndexOf("/ClientBin")) + "/" + e.Result[i].Path);
                            this.sp1.Children.Add(lnk);
                        }
                        else
                        {
                            TextBlock tbk = new TextBlock();
                            tbk.FontSize = 14;
                            tbk.Text = (i + 1).ToString() + "." + e.Result[i].Title;
                            this.sp1.Children.Add(tbk);
                        }
                    }
                }
            }
            else
            {
                throw e.Error;
            }
        }

        private void lnkbtnLogout_Click(object sender, RoutedEventArgs e)
        {
            CSession.Session["login"] = "";
            this.spLogin.Visibility = Visibility.Visible;
            this.tbkUserName.Text = "";
            this.spLogin1.Visibility = Visibility.Collapsed;
            InitFileList();
        }
    }
}
